package com.team.mpc.controller;

import com.team.mpc.service.OauthCodeService;
import com.team.mpc.service.impl.LocalServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.client.producer.TransactionSendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
public class PushController {
    @Autowired
    private OauthCodeService oauthCodeService;

    @PreAuthorize("hasAuthority('a')")
    @RequestMapping("/push/{message}")
    public String push(@PathVariable("message") String message){
        return message;
    }

    @RequestMapping("test")
    public String test(String a){
        int s =1/0;
        return a;
    }

    @Autowired
    private RocketMQTemplate rocketMQTemplate;

    @RequestMapping("p/{msg}")
    public String produce(@PathVariable("msg")String msg){

        rocketMQTemplate.convertAndSend("my-topic",msg);
        //同步消息                   第一个参数使用:将topic和tag分隔
        SendResult syncMsg = rocketMQTemplate.syncSend("my-topic:tag", "同步消息", 10000);
        //异步消息
        rocketMQTemplate.asyncSend("my-topic:tag", "异步消息", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("成功发送");
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("发送失败");
            }
        });
        //单向发送
        rocketMQTemplate.sendOneWay("my-topic:tag","单向消息");
        //顺序消息  实现就是将消息值发送到一个messagequeue，否则消息会按照规则放到多个messagequeue，因此无法保证消费顺序
        SendResult syncMsg1 = rocketMQTemplate.syncSendOrderly("my-topic:tag", "同步消息","hashkey",10000);
        rocketMQTemplate.asyncSendOrderly("my-topic:tag", "异步消息","hashkey", new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                log.info("成功发送");
            }

            @Override
            public void onException(Throwable throwable) {
                log.info("发送失败");
            }
        });
        rocketMQTemplate.sendOneWayOrderly("my-topic:tag","单向消息","hashkey");
        return "消息已发送";
    }


    //事务消息  需要结合SendMsgListener该类使用
    @RequestMapping("ptx/{msg}")
    public String produceTx(@PathVariable("msg")String msg){
        //发送半事务消息
        TransactionSendResult tx_p = rocketMQTemplate.sendMessageInTransaction("tx_p",
                "my-topic:tx",
                MessageBuilder.withPayload(msg).setHeader("txId","给定本地事务id").build(),
                msg);
        return "事务消息";
    }


    @RequestMapping("seata")
    public String seata(@RequestParam("a") String a){
        log.info("mpc呗访问");
       oauthCodeService.testUpdate();
        return "已变更";
    }


    @PreAuthorize("hasAuthority('all')")
    @RequestMapping("oauth")
    @ResponseBody
    public Object oauth(){
        return SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    }


}
